<template>
	<view class="bgColor-white">
		<cu-custom :key="'header'+headerKey" bgColor="bg-gradual-green" title="就业创业码" :isBack="true" backText="返回"></cu-custom>
		<view class="padding-top:30rpx">
		<view class="text-center padding text-xxl">
			<text>{{ fullName }}的就业创业码</text>
		</view>
		<view class="text-gray text-center text-lg">
			<text>就业创业码将显示您的就业信息，请妥善保管</text>
		</view>
		<view class="text-center padding">
			<view v-if="codeBase64"><image :src="codeBase64" style="width: 600rpx;height: 600rpx;"></image></view>
		</view>
		<view v-if="msg" class="text-center text-xl">
			<text>{{msg}}</text>
		</view>
		<view class="text-gray text-center padding text-df">
			<text>更新时间：{{currentTime}}</text>
			<text v-if="codeClick" style="padding-left: 10rpx;" class="basefont icon-reload" @click="refresh"></text>
		</view>
		</view>
	</view>
</template>

<script>
	import store from '@/store';
	import api from '@/api/system/qrcode.js'
	import miment from 'miment'
	export default {
		onShow() {
			this.headerKey += 1;
		},
		data() {
			return {
				headerKey:0,
				fullName: store.state.hh_user.fullName,
				accessToken: store.state.hh_token,
				reloadTimer: '', //刷新二维码定义一个定时器的变量
				currentTime: miment().format('YYYY年MM月DD日 hh:mm:ss'), // 获取当前时间
				codeClick: true,
				codeText: '获取二维码失败',
				codeBase64: null,
				msg:null
			}
		},
		onReady() {
			this.initCodeUrl();
		},
		onLoad() {
		},
		methods: {
			refresh() {
				clearInterval(this.reloadTimer);
				this.initCodeUrl();
			},
			initCodeUrl() {
				const vm = this;
				if (this.codeClick) {
					const data ={}
					// #ifdef MP-WEIXIN
					data.applyType = '3'
					// #endif
					// #ifdef H5
					data.applyType = '2'
					// #endif
					// #ifdef MP-ALIPAY
					data.applyType = '4'
					// #endif
					this.$http.post(api.getPersonCodeBase64, data).then(res => {
						const result = res.result
						this.codeBase64 = result.imgBase64
						this.msg =result.msg
						this.codeClick = false;
						this.currentTime = miment().format('YYYY年MM月DD日 hh:mm:ss');
						// 倒计时开始
						let daojishi = 300;
						this.reloadTimer = setInterval(function() {
							daojishi--;
							if (daojishi > 0 &&daojishi<=290) {
								vm.codeClick = true;
							}else if (daojishi <= 0) {
								vm.codeClick = true;
								clearInterval(vm.reloadTimer);
								vm.initCodeUrl();
							}
						}, 1000);
					}).catch(error => {
						
					})					
					
				}
			}
		
		},
		beforeDestroy() {
			if (this.reloadTimer) {
				clearInterval(this.reloadTimer); // 在Vue实例销毁前，清除我们的定时器
			}
		}
	}
</script>

<style>

</style>
